<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use baseutils\BaseUtil; # 验证类
use com\gdl\newagk\interfaces\AdminSectionsServiceClient;
use com\gdl\newagk\dataobject\adminsections\SectionsDTO; 

/**
 * 章节表
 * @author tc 2018/05/07
 */

class Adminsections extends Base
{
 	/** 
 	 *  章节   
     *  @param  admin_reg or plaformId
 	 *  @return JSON [description] 400失败 200成功
 	 */
	public function sectionsList() 
    {

    	 # 获取该平台下子平台和当前平台的数据
        $allAdminReg = getAllplaformReg();
        if($plaformId==0 || empty($plaformId)){ # admin 总平台
            foreach($allAdminReg as $key){
                $adminReg[] = $key['admin_reg'];    
            }
            
        }else{ # 子平台所有公司
            $adminReg = getChildPloformReg($allAdminReg,$plaformId);
        }

        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $api = new AdminSectionsServiceClient(NULL);
            \ApiClient::build($api);
            // var_dump($api);exit;
            $DataDO = new SectionsDTO(); 
            
            
            $DataDO->offset = $offset;
            $DataDO->num = $num; 
            # 平台精确查询
            if($plaformChildId){
                $adminReg = getSelectPlaformReg($plaformChildId);
            }
            $adminReg = implode("','",$adminReg);
            
            $where = " isdelete = 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                $where .= " and name like '%".$liekName."%' ";
            }
            $DataDO->where = $where; 
            $result = $api->sectionsList($DataDO);  

            if ($result->success == TRUE) {
            	$msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }
}